<?php
class memberControl extends fnControl {
	//现实会员列表
	public function index(){	
		$db = K('user_role');
		$data = $db->all();
		$this->assign('data', $data);		
		$this->display();		
	}
	//公用添加方法,以post参数 rbacType区分不同表.
	public function add(){
		$rbacTable = $_POST['rbacType'];
		$db =  M($rbacTable);
		if($db->insert()){
			$this->success('添加成功');
		}else{
			$this->error('添加失败');
		}		
	}
	//添加用户显示界面
	public function add_user_show(){
		$db = M('role');
		$role = $db->field('rid,rname')->all();
		$this->assign('role',$role);
		$this->display();
	}
	//具体添加用户方法
	public function add_user(){
		$this->verify();
		$db = M('user');
		$_POST['password'] = md5($_POST['password']);
		if($uid = $db->add()){
			$_POST['uid']  =$uid;
			if($db->table('user_role')->add()){
				$this->success('添加成功');
			}else{
				$this->success('用户组添加失败');
			}
		}else{
			$this->error('用户添加失败');
		}		
	}
	//rbac添加节点显示界面
	public function add_node_show(){
		$db =  M('node');
		$node = $db->field('name,level,nid')->where('level<3')->select();		
		$this->assign('node', $node);		
		$this->display();	
	}
	//rbac添加用户组显示界面
	public function add_role_show(){		
		$this->display();
	}
	//rbac权限分配显示界面
	public function add_access_show(){
		$db = M('node');
		$node = $db->field('nid,pid,level,title,name')->all();
		$role = $db->table('role')->field('rid,title')->all();
		$node = arr::channel($node,'nid','pid',0,0,1,'>');						
		$this->assign('node', $node);
		$this->assign('role', $role);
		$this->display();
	}
	//显示rbac配置信息页面
	public function show_rbac(){
		$db = M('role');
		$rid = $db->field('rid')->select();
		$db = K('access');
		$resultArr = array();
		
		foreach ($rid as $v){
			$data = $db->order('node.level')->where('role.rid='.$v['rid'])
			->field('access.rid,role.rname,role.title,node.nid,node.name,node.title,node.pid,node.level')
			->select();		
			if($data){
				$data = arr::channel($data,'nid','pid',0,0,2,'');
				
				foreach ($data as $v){
					$resultArr[] = $v;
				}
			}						
		}		

		$this->assign('result', $resultArr);
		$this->display();
	}
	//具体实现权限分配方法。 
	public function add_access(){		
		$db = M('access');
		$rid = $_POST['rid'];
		$state = $db->where('rid='.$rid)->select();
		if(!isset($_POST['nid'])){
			$this->error('没有进行任何的权限设置');
		}
		if($state){
			$db->where('rid='.$rid)->delete();
		}	
		foreach ($_POST['nid'] as $v){
			$data = array(
				'rid'=>$rid,
				'nid'=>$v		
			);
			if(!$db->add($data)){
				$this->error('配置失败');
			}									
		}
		$this->success('配置成功');		
	}
	//具体实现用户组编辑的方法
	public function edit_role(){
		C('DEBUG',0);
		$db = M('user_role');
		$uid = $_POST['uid'];
		$rid = $_POST['rid'];
		if($db->where('uid='.$uid)->save()){
			$result = $db->table('role')->field('rname,title')->where('rid='.$rid)->find();
			if($result){
				$result['state'] = 1;
				echo json_encode($result);
				$_POST = array();
			}			
		}		
	}
	//显示用户组，用于会员管理页ajax的修改
	public function show_role(){
		$db = M('role');
		$role = $db->field('rid,rname')->all();
		echo json_encode($role);	
	}
	//显示用户组节点信息，用于ajax修改
	public function node_info(){
		$db = M('access');
		if(!isset($_POST['rid']))return;		
		$data = $db->field('nid')->select('rid='.$_POST['rid']);
		$result = array();
		if($data){
			$result['state'] = true;
			$result['nid'] = $data;
		}
		echo json_encode($result);
	}	
}

?>